package com.example.emos.api.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.controller.form.InsertExamForm;
import com.example.emos.api.db.dao.TbExamDao;
import com.example.emos.api.db.pojo.TbExam;
import com.example.emos.api.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author ：Supyo
 * @date ：Created in 2021/11/13 23:35
 * @description：
 */
@Service
public class ExamServiceImpl implements ExamService {
    @Autowired
    private TbExamDao examDao;

    @Override
    public TbExam selectCheckListById(Integer id) {
        TbExam map = examDao.selectCheckListById(id);
        return map;
    }

    @Override
    public ArrayList<HashMap> getUser() {
        ArrayList<HashMap> map = new ArrayList<HashMap>();
        map = examDao.getUser();
        return map;
    }


    @Override
    public PageUtils selectCheckListBypagesForm(HashMap param) {
        ArrayList<HashMap> list = examDao.selectCheckListBypages(param);
        long count = examDao.countCheckList();
        int start = (Integer) param.get("start");
        int length = (Integer) param.get("length");
        PageUtils pageUtils = new PageUtils(list, count, start, length);

        //修改状态
        LocalDateTime time = LocalDateTime.now().withNano(0);
        System.out.println(time);
        for (int i = 0; i < list.size(); i++) {
            //获取考核时间
            LocalDateTime startTime[] = new LocalDateTime[list.size()];
            startTime[i] = (LocalDateTime) list.get(i).get("start_time");
            LocalDateTime endTime[] = new LocalDateTime[list.size()];
            endTime[i] = (LocalDateTime) list.get(i).get("end_time");
            System.out.println("startTime:  " + startTime[i]);
            System.out.println("endTime:  " + endTime[i]);
            System.out.println("start " + time.isBefore(startTime[i]));
            System.out.println("time.isAfter: " + time.isAfter(endTime[i]));
            if (time.isBefore(startTime[i])) {
                int status = 1;
                examDao.updateStatus((int) list.get(i).get("id"), status);
            }
            if (time.isAfter(startTime[i]) && time.isBefore(endTime[i])) {
                int status1 = 2;
                examDao.updateStatus((int) list.get(i).get("id"), status1);
            }
            if (time.isAfter(endTime[i])) {
                int status1 = 3;
                examDao.updateStatus((int) list.get(i).get("id"), status1);
            }

        }

        return pageUtils;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(TbExam exam) {
        int rows = examDao.insert(exam);
        return rows;
    }


    @Override
    public int deleteExamByIds(Integer[] ids) {
        int rows = examDao.deleteExamByIds(ids);
        //删除考核时，级联删除被考核表，考核表，考核任务表的数据
        for (int i = 0; i < ids.length; i++) {
            examDao.deleteExamniee(ids[i]);
            examDao.deleteMark(ids[i]);
            examDao.deleteTask(ids[i]);
        }
        return rows;
    }

    @Override
    public int update(HashMap param) {
        int rows = examDao.update(param);
        return rows;
    }

}
